package com.example.controller.mobilecontroller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.config.ResponseVO;
import com.example.pojo.EnrollInfo;
import com.example.pojo.Notification;
import com.example.pojo.NotificationReadStatus;
import com.example.pojo.StuInfo;
import com.example.pojo.dto.NotificationDTO;
import com.example.service.NotificationService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/mobile")
public class NotificationController {
    @Resource
    private NotificationService notificationService;
    @GetMapping("/notificationList")
    public ResponseEntity<ResponseVO<List<Notification>>> getEnrollInfoList() {
        List<Notification> notifications = notificationService.notificationList();
        if (!notifications.isEmpty()) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "报名信息获取成功", notifications));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "暂无报名信息数据", null));
    }

//    分页查询
    @GetMapping("/notificationListPage/{currentPage}/{limit}")
    public ResponseEntity<ResponseVO<IPage<Notification>>> getNotificationInfoPage(@PathVariable int currentPage, @PathVariable int limit) {
        IPage<Notification>  enrollInfoIPage = notificationService.getNotificationInfoPage(currentPage, limit);
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "用户信息获取成功", enrollInfoIPage));
    }
//    通过标题查询
    @GetMapping("/getNotificationByTitleInfoPage/{currentPage}/{limit}/{title}")
    public ResponseEntity<ResponseVO<IPage<Notification>>> getNotificationByTitleInfoPage(@PathVariable int currentPage, @PathVariable int limit, @PathVariable String title){
        IPage<Notification>  enrollInfoIPage = notificationService.getNotificationByTitleInfoPage(currentPage,limit,title);
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,"通知获取成功",enrollInfoIPage));
    }

//    添加&修改
    @PostMapping("/addOrUpdateNotification")
    public ResponseEntity<ResponseVO<String>> addOrUpdateNotification(@RequestBody Notification notification) {
        String result = notificationService.addOrUpdateNotification(notification);
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, result));
    }

//    添加操作
    @PostMapping("/addNotificationForAllParents")
    public ResponseEntity<ResponseVO<String>> addNotificationForAllParents(@RequestBody Notification notification) {
        String result = notificationService.addNotificationForAllParents(notification);
        System.out.println("这是图片路径："+notification.getImage());
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, result));
    }

    @PostMapping("/isReadNotification")
    public ResponseEntity<ResponseVO<List<NotificationDTO>>> isReadNotification(@RequestBody NotificationReadStatus notificationReadStatus){
        String readNotification = notificationService.isReadNotification(notificationReadStatus);
        if("已阅".equals(readNotification)){
            List<NotificationDTO> notificationByUserId = notificationService.getNotificationByUserIdAndType(notificationReadStatus.getUserId(),0);
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "已阅读", notificationByUserId));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "请稍后再试", null));
    }

    //删除
    @PostMapping("/deleteNotificationById")
    public ResponseEntity<ResponseVO<String>> deleteEnrollInfo(@RequestBody Notification notification) {
        int resultRow = notificationService.deleteNotificationFor(notification);
        if (resultRow > 0) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "通知删除成功", "success"));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.NOT_FOUND, "系统繁忙，请稍后再试", null));
    }

    //批量删除
    @DeleteMapping("/batchNotificationByIds")
    public ResponseEntity<ResponseVO<String>> batchDeleteNotificationInfo(@RequestParam List<Long> notificationArr){
        try {
            int result = notificationService.batchNotificationInfo(notificationArr);
            if(result>0){
                return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,"删除成功"));
            }else{
                return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST,"删除用户失败,请稍后再试"));
            }
        } catch (Exception e) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST,"删除用户失败 " + e.getMessage()));
        }
    }

    @GetMapping("/getNotificationByUserIdAndType/{userId}/{notificationType}")
    public ResponseEntity<ResponseVO<List<NotificationDTO>>> getNotificationByUserIdAndType(@PathVariable Long userId,@PathVariable Integer notificationType){
        List<NotificationDTO> notificationByUserId = notificationService.getNotificationByUserIdAndType(userId,notificationType);
        if (!notificationByUserId.isEmpty()) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "通知信息获取成功", notificationByUserId));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "暂无信息数据", null));
    }

    // 获取未读的通知
    @GetMapping("/getNotReadNotificationByUserIdAndType/{userId}/{notificationType}")
    public ResponseEntity<ResponseVO<List<NotificationDTO>>> getNotReadNotificationByUserIdAndType(@PathVariable Long userId,@PathVariable Integer notificationType){
        List<NotificationDTO> notificationByUserId = notificationService.getNotReadNotificationByUserIdAndType(userId,notificationType);
        if (!notificationByUserId.isEmpty()) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "未读通知信息获取成功", notificationByUserId));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "暂无信息数据", null));
    }
}
